#!usr/bin/env python  
# -*- coding: utf-8 -*-#

#-------------------------------------------------------------------------------
# Name:         pygame1
# Description:  
# Author:       dyg
# Date:         2018/2/8
#-------------------------------------------------------------------------------

import pygame
from pygame.locals import *
import sys
from random import randint

#定义窗口分辨率
SCREEN_WIDTH = 579
SCREEN_HEIGHT = 578


# 设置上下左右
offset = {pygame.K_LEFT:0,pygame.K_RIGHT:0,pygame.K_UP:0,pygame.K_DOWN:0}
#定义窗口分辨率
FRAME_RATE = 60
#定义动画周期帧数
ANIMATE_CYCLE = 30
clock = pygame.time.Clock()
ticks = 0

#敌机
class Enemy(pygame.sprite.Sprite):
    def __init__(self,enemy_surface,enemy_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = enemy_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = enemy_init_pos
        self.speed = 2
        self.down_index = 0

    def update(self):
        self.rect.top += self.speed
        if self.rect.top > SCREEN_HEIGHT:
            self.kill()



#子弹
class Bullet(pygame.sprite.Sprite):
    def __init__(self,bullet_surface,bullet_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = bullet_init_pos
        self.speed = 8

    #控制子弹移动
    def update(self):
        self.rect.top -= self.speed
        if self.rect.top < -self.rect.height:
            self.kill()

# 玩家
class Hero(pygame.sprite.Sprite):
    def __init__(self,hero_surface,hero_init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = hero_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = hero_init_pos
        self.speed = 6
        self.bullet1 = pygame.sprite.Group()  #子弹1的group
        self.is_hit = False   #是否被击中，默认为false


    #射击
    def single_shoot(self,bullt1_surface):
        bullet1 = Bullet(bullt1_surface,self.rect.midtop)
        self.bullet1.add(bullet1)


    #移动
    def move(self,offset):
        # 固定边框
        hero_x =self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
        hero_y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
        if hero_x < 0:
            self.rect.left = 0
        elif hero_x > SCREEN_WIDTH - self.rect.width:
            self.rect.left= SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left = hero_x

        if hero_y < 0:
            self.rect.top = 0
        elif hero_y > SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top = hero_y

#初始化游戏
pygame.init()   #初始化pygame
screen = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])  #初始化一个用于显示窗口
pygame.display.set_caption('第一个游戏')  #设置窗口标题


background = pygame.image.load('resources/image/bj.jpg')  #载入背景图片
shoot_img  = pygame.image.load('resources/image/life.png')  #载入飞机图片
shoot_down_img  = pygame.image.load('resources/image/laserRedShot.png')#l载入飞机坠毁图片
bullet_img  = pygame.image.load('resources/image/laserGreen.png')  #载入子弹图片
enemy_img  = pygame.image.load('resources/image/meteorSmall.png')  #载入敌机图片
bomb_img  = pygame.image.load('resources/image/laserGreenShot.png')  #载入敌机坠毁图片
gameover_img  = pygame.image.load('resources/image/gameover.jpg')  #游戏结束图片

#用subsurface 剪切读入的图片
hero_surface = []
hero1_rect = pygame.Rect(0,0,35,27)
hero1 = shoot_img .subsurface(hero1_rect)
hero_surface.append(hero1)
hero_pos = [272,500]

bullet1_surfae = bullet_img.subsurface(pygame.Rect(0,0,5,33))
enemy1_surface = enemy_img.subsurface(pygame.Rect(0,0,44,42))
bomb1_surface_group = []
bomb1_surface = bomb_img.subsurface(pygame.Rect(0,0,56,54))
bomb2_surface = bomb_img.subsurface(pygame.Rect(28,27,0,0))
bomb1_surface_group.append(bomb1_surface)
bomb1_surface_group.append(bomb2_surface)

shoot_down_img_group = []
shoot_down1_surface = shoot_down_img.subsurface(pygame.Rect(0,0,56,54))
shoot_down2_surface = shoot_down_img.subsurface(pygame.Rect(28,27,0,0))
shoot_down_img_group.append(shoot_down1_surface)
shoot_down_img_group.append(shoot_down2_surface)



#创建玩家
hero = Hero(hero_surface[0],hero_pos)

#创建敌机精灵组
enemy_group = pygame.sprite.Group()
#创建摧毁敌机精灵组
enemy_down_group = pygame.sprite.Group()

hero_down_index = 0

#事件循环（main，loop）
while True:

    #限制游戏最大帧率
    clock.tick(FRAME_RATE)
    #绘制背景
    screen.blit(background,(0,0))
    #射击  频率
    if ticks % 30 == 0 :
        hero.single_shoot(bullet1_surfae)
    #控制子弹
    hero.bullet1.update()
    #绘制子弹
    hero.bullet1.draw(screen)

    ticks += 1

    # 绘制飞机
    if hero.is_hit:
        if ticks % (ANIMATE_CYCLE // 2) == 0:
            hero_down_index += 1
        hero.image = shoot_down_img_group[hero_down_index]
        if hero_down_index == 1:
            break
    else:
        hero.image = hero_surface[0]

    screen.blit(hero.image,hero.rect)

    # 生产敌机
    if ticks % 30 == 0:
        enemy = Enemy(enemy1_surface,[randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
        enemy_group.add(enemy)

    # 控制敌机
    enemy_group.update()
    # 绘制敌机
    enemy_group.draw(screen)


    # 检测敌机与子弹的碰撞
    enemy_down_group.add(pygame.sprite.groupcollide(enemy_group, hero.bullet1, True, True))

    for enemy1_down in enemy_down_group:
        screen.blit(bomb1_surface_group[enemy1_down.down_index],enemy1_down.rect)
        if ticks % (ANIMATE_CYCLE//2) == 0:
            if enemy1_down.down_index < 1:
                enemy1_down.down_index += 1
            else:
                enemy_down_group.remove(enemy1_down)

    # 检测敌机与玩家之间的碰撞
    enemy_down_list = pygame.sprite.spritecollide(hero, enemy_group, True)
    if len(enemy_down_list) > 0:
        enemy_down_group.add(enemy_down_list)
        hero.is_hit = True

    # 更新屏幕
    pygame.display.update()

    #处理游戏推出
    #从消息队列中循环获取
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        #控制方向
        if event.type == pygame.KEYDOWN:
            if event.key in offset:
                offset[event.key] = hero.speed
        elif event.type == pygame.KEYUP:
            if event.key in offset:
                offset[event.key] = 0

    # 移动飞机
    hero.move(offset)


#跳出主循环
screen.blit(gameover_img,(0,0))
while True:
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()


